package sg.edu.nus.iss.vmcs.simulation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import sg.edu.nus.iss.vmcs.abstraction.ISimulationControlPanel;
import sg.edu.nus.iss.vmcs.abstraction.Messages;
import sg.edu.nus.iss.vmcs.abstraction.PresenterBase;
import sg.edu.nus.iss.vmcs.abstraction.PresenterNames;
import sg.edu.nus.iss.vmcs.machinery.MachineryController;
import sg.edu.nus.iss.vmcs.maintenance.MaintenanceController;
import sg.edu.nus.iss.vmcs.system.MainController;
import sg.edu.nus.iss.vmcs.util.VMCSException;

/*
 * @author Nyi Nyi Than
 */

public class SimulationControlPanelPresenter extends
		PresenterBase<ISimulationControlPanel>
{
	WindowListener closingListener;
	ActionListener beginSimulationListener;
	ActionListener activateCustomerListener;
	ActionListener activateMaintainerListener;
	ActionListener activateMachineryListener;
	ActionListener endSimulationListener;

	public SimulationControlPanelPresenter(String name)
	{
		super(name);
		InitializeListener();	
		PresenterBase.mediator.register(this);
	}

	private void InitializeListener()
	{
		this.closingListener = new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				closeDown();
			}
		};

		this.beginSimulationListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{				
				view.setSimulationActive(true);
			}
		};

		this.endSimulationListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				closeDown();	
			}
		};

		this.activateCustomerListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				send(PresenterNames.CUSTOMER_PRESENTER, Messages.OPEN);
				view.setButtonState(ISimulationControlPanel.ACT_CUSTOMER, false);
			}
		};
		
		this.activateMaintainerListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{										
				MainController mctrl = new MainController();
				try
				{
					mctrl.getMaintenanceController().displayMaintenancePanel();
				} catch (VMCSException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				view.setButtonState(ISimulationControlPanel.ACT_MAINTAINER, false);	
			}
		};
		
		this.activateMachineryListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				MainController mctrl = new MainController();
				mctrl.getMachineryController().displayMachineryPanel();
				view.setButtonState(ISimulationControlPanel.ACT_MACHINERY, false);
			}
		};
	}

	public WindowListener getClosingListener()
	{
		return this.closingListener;
	}

	public ActionListener getBeginSimulationListener()
	{
		return this.beginSimulationListener;
	}

	public ActionListener getEndSimulationListener()
	{
		return this.endSimulationListener;
	}

	public ActionListener getActivateMaintainerListener()
	{
		return this.activateMaintainerListener;
	}
	
	public ActionListener getActivateCustomerListener()
	{
		return this.activateCustomerListener;
	}
	
	public ActionListener getActivateMachineryListener()
	{
		return this.activateMachineryListener;
	}

	@Override
	public void display()
	{
		view.setSimulationActive(false);
		this.view.display();
	}

	@Override
	public void notify(PresenterBase<?> sender, String message)
	{
		// TODO to implement handler based on message sent from others
	}

	@Override
	public void send(String receiverName, String message)
	{
		mediator.sendMessage(this, receiverName, message);
	}

	@Override
	public void closeDown()
	{
		view.close();
		send(PresenterNames.CUSTOMER_PRESENTER,Messages.CLOSE);
		
		closeOldSystemPanels();
	}

	private void closeOldSystemPanels()
	{
		MainController mctrl = new MainController();
		MachineryController mc = mctrl.getMachineryController();
		if(mc!=null)mc.closeDown();
		mctrl.getMachineryController().closeDown();
		mctrl.getMaintenanceController().closeDown();
	}

}
